﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using QLTT.Models;
using QLTT.Commands;
using System.Windows.Input;
using System.Text.RegularExpressions;
using QLTT.Properties;

namespace QLTT.ViewModels
{
    class AbsenceViewModel : WorkspaceViewModel
    {
        private Absence _absence;
        private icanDataContext _dataContext;
        private DelegateCommand _saveCommand;
        private DelegateCommand _editCommand;
        private DelegateCommand _cancelCommand;

        private bool _isSelected;
        private bool _isChecked;
        public AbsenceViewModel(icanDataContext dataContext)
        {
            _dataContext = dataContext;
            _absence = new Absence();
            _absence.Date = DateTime.Today;
            this.IsEnabled = true;
            base.DisplayName = Properties.Resources.Base_DisplayName_Absence_New;
        }
        public AbsenceViewModel(icanDataContext dataContext, StudentClass sc, DateTime date)
        {
            _dataContext = dataContext;
            _absence = new Absence();
            _absence.Date = date;
            //_absence.StudentClass1 = sc;
            _absence.StudentClass = sc.ID;
            

            this.IsEnabled = true;
            this.IsChecked = false;
            base.DisplayName = Properties.Resources.Base_DisplayName_Absence_New;
        }

        public AbsenceViewModel(icanDataContext dataContext, Absence absence)
        {
            _absence = absence;
            _dataContext = dataContext;
            this.IsEnabled = false;
            this.IsChecked = true;
            base.DisplayName = Properties.Resources.Base_DisplayName_Absence_Edit;
        }
        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new DelegateCommand(Save);
                }
                return _saveCommand;
            }
        }
        public ICommand EditCommand
        {
            get
            {
                if (_editCommand == null)
                {
                    _editCommand = new DelegateCommand(Edit);
                }
                return _editCommand;
            }
        }
        public ICommand CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                {
                    _cancelCommand = new DelegateCommand(Cancel);
                }
                return _cancelCommand;
            }
        }

        public void Save()
        {
            if (_dataContext == null || _absence == null)
                return;
            try
            {
                bool isInsert = _absence.ID == 0;
                if (isInsert)
                {
                    _dataContext.Absences.InsertOnSubmit(_absence);
                }
                _dataContext.SubmitChanges();
                if (isInsert)
                    Mediator.NotifyColleagues<Absence>(Messages.ListAbsenceUpdated, _absence);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("Application", ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
            }

        }
        private void Edit()
        {
            if (_dataContext == null || _absence == null)
                return;

            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceAdd, this);
        }
        public void Delete()
        {
            if (_dataContext == null || _absence == null)
                return;
            try
            {
                _dataContext.Absences.DeleteOnSubmit(_absence);
                _dataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("Application", ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
            }
        }
        
        public void Cancel()
        {
            if (_absence.ID > 0)
                _dataContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, _absence);
           
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceRemove, this);
        }
        public long ID
        {
            get { return _absence.ID; }
        }

        public long StudentClass
        {
            get { return _absence.StudentClass; }
            set
            {
                if (_absence.StudentClass == value) return;
                _absence.StudentClass = value;
                base.OnPropertyChanged("StudentClass");
            }
        }
        public string Reason
        {
            get { return _absence.Reason; }
            set
            {
                if (_absence.Reason == value) return;
                _absence.Reason = value;
                base.OnPropertyChanged("Reason");
            }
        }
        public long Student
        {
            get
            {
                return _dataContext.StudentClasses.Where(st1 => st1.ID==_absence.StudentClass).Select(st => st.Student).FirstOrDefault();
            }
        }
        public string StudentName
        {
            get { return _dataContext.Students.Where(s => s.ID == _absence.StudentClass).Select(s => s.Name).FirstOrDefault(); }
        }
        public string ClassName
        {
            get {
                long classID = _dataContext.StudentClasses.Where(st => st.ID == _absence.StudentClass).Select(st => st.Class).FirstOrDefault();
                return _dataContext.Classes.Where(c => c.ID == classID).Select(c => c.Name).FirstOrDefault();
            }
        }
        public DateTime Date
        {
            get { return _absence.Date; }
            set
            {
                if (_absence.Date == value) return;
                _absence.Date = value;
                base.OnPropertyChanged("Date");
            }
        }

        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (value == _isSelected)
                    return;

                _isSelected = value;

                base.OnPropertyChanged("IsSelected");
            }
        }
        public bool IsChecked
        {
            get { return _isChecked; }
            set
            {
                if (value == _isChecked)
                    return;

                _isChecked = value;

                base.OnPropertyChanged("IsChecked");
            }
        }

        public bool IsEnabled { get; set; }
    }
}
